import {
	api_getLineList1,
	api_getLineList2
} from '../../api/api.transit.js'
export default {
	namespaced: true,
	state: {
		site:null,
		lineName: "",
		lineId: "",
		lineList: [],
	},
	mutations: {
		SET_SITE(state, data) {
			state.site = data
		},
		SET_LINE_NAME(state, data) {
			state.lineName = data
		},
		SET_LINE_ID(state, data) {
			state.lineId = data
		},
		SET_LINE_LIST(state, data) {
			state.lineList = data
		}
	},
	actions: {
		GET_LINE_LIST({ state, commit }) {
			const data = {
				lineSiteId: state.site._id
			}
			return new Promise((resolve, reject)=>{
				api_getLineList1(data)
					.then((res) => {
						if (res.showapi_res_body.lineList) {
							let body = res.showapi_res_body.lineList
							commit("SET_LINE_LIST", body)
							resolve(body)
						} else {
							commit("SET_LINE_LIST", [])
							reject()
						}
					})
					.catch(() => {
						commit("SET_LINE_LIST", [])
						reject()
					})
			})
		},
		GET_LINE_ID({ rootState,state, commit }) {
			return new Promise((resolve, reject)=>{
				const data = {
					cityName:rootState.CityName,
					lineName: state.lineName
				};
				api_getLineList2(data)
					.then((res) => {
						if (res.showapi_res_body.soulist[0]) {
							let id = res.showapi_res_body.soulist[0]._id
							commit("SET_LINE_ID", id)
							resolve(id)
						} else {
							commit("SET_LINE_ID", "")
							reject()
						}
					})
					.catch(() => {
						commit("SET_LINE_ID", "")
						reject()
					})
			})
		},
		PASS_LINE({commit},id){
            commit('lineDetails/SET_LINE_ID', id, {root: true}) 
        },
	}
}
